package com.wanmait.computershop.webController;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

//支付宝支付接口
@Controller
@RequestMapping("/pay")
public class PayTest {


    private String serverUrl="https://openapi-sandbox.dl.alipaydev.com/gateway.do";
    private String appId="9021000143657222";
    private String privateKey="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCtQyYlKjLXBQes3S66uDIB6q0OXMO/RsLI9jn5bJHwyY0CeUjbtYMsVC/qbMLvZblX+JRZGw8hfH6FHrM2lpLnXsxgcJ5N9/eUxAXENwwhqg8L8KpakcSZWAunHoWx+f7Nv3z1Hw5rdm8wjmFLpmnSYfBwPGd5DLy9SRHcz9chfCgOjIh/FZQmODAUESNrumOMlqDtU1UNqmyRwlW+XVyr3F4xlS3Lm3fv1+RECbicW/vOTf6bS5j1EP/Op072Ue3QGxowp34mgJ3NKdcTzkM3a1Ugf1xNJTQs1dObS7jRvTDJLD/BKypz56TlbQPZf2J9W9+NxoHDFQACS36TlGR3AgMBAAECggEAB6NDJjZDAXmGSheB/Ncr1SvMLyrlsiPjaEp6acVgdZXc8nP4Qz8wJh8b8aCTw0vfrMIXgAoSU66SQToGOmzO0wY5yjKMt/ErZ9JwUqL+lOlTOnqEd/OfEAdylA5jZGIZ2mYLkx6oWxJgemp8XYJbfdu1Zq0y/oISO+HhuLMsz5v/xPN9ip27SH17Rw+fq6p13zPBjWwpM3Y6Ed5LPH4mD8jv6SonDEvc6QTuEgVObzY8CG9P9ArisswUlDGceHmwu2W+orGtFBlilBf/drbCeSp3PFKQqN6MqoHflAMZ2oDaujPWCq2jRLpt1NKOTdUWHDw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aY+icVnV/DIzfoSicjM1fbFmj3UVRB8rNa/Zj4l4yCCtfodZV+kLuNnozPeRIkjC9b/jHoNFzaibfXE3478i6eqUHYEggXei1BBfePLcAppEDrT8subnRwgV0IVN71OFdb/xsNuuVi6MAvUr9b7tANlKOYHd4mTCHj9fNNpESaUZgpIsOczD65Q96xtQBByTYRcluhAwhh9bJTBtaU7W+LsgUMTL46K6XYySFofwIDAQAB";

    //创建订单接口
    @ResponseBody
    @GetMapping("/create")
    public String create(){
        //创建连接
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl,appId,privateKey,"json", "UTF-8",alipayPublicKey,"RSA2");
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();

        //订单信息
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "202100012260166");//订单号
        bizContent.put("total_amount", 5.00);//订单金额
        bizContent.put("subject", "test");//订单名称
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");

        request.setBizContent(bizContent.toString());

        AlipayTradePagePayResponse response = null;
        try {
            response = alipayClient.pageExecute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if(response.isSuccess()){

            //成功
            return  response.getBody();
        } else {
            //失败
            return  "调用失败";
        }

    }
}